<?php
class user_controller extends CI_Controller {

	public function __construct()
	{
		parent::__construct();	
		if($this->session->userdata('logged_in'))
   		{
			$this->load->model('user_model');
		}
   		else
   		{
     		//If no session, redirect to login page
     		redirect('welcome', 'refresh');
   		}
	}

	public function index()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'admin'))
   		{		
			$data['user'] = $this->user_model->get_user();
			$data['title'] = 'user archive';
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('users/userindex_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}		
	}
	
	public function create()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Username', 'Username', 'trim|required|xss_clean');
		$this->form_validation->set_rules('Password', 'Password', 'trim|required|xss_clean');

		if ($this->form_validation->run() === FALSE)
		{
			$this->create_view();
		}
		else
		{
			$this->user_model->set_user();
			$this->index();
		}
	}
	
	public function create_view()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'admin'))
   		{	
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('users/usercreate_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
	
	public function validate_view()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'admin'))
   		{
			$userid = $this->input->post('userid');
			$data['user'] = $this->user_model->get_user_id($userid);
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('users/uservali_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
	
	public function changeadmin_view()
	{
		$user_data = $this->session->userdata('logged_in');
		if($this->session->userdata('logged_in')  && ($user_data['role'] == 'admin'))
   		{
   			$user_data = $this->session->userdata('logged_in');
   			$userid = $user_data['id'];
			$data['user'] = $this->user_model->get_user_id($userid);
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('users/changeadmin_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
	
	public function changelehrer_view()
	{
		$user_data = $this->session->userdata('logged_in');
		if($this->session->userdata('logged_in')  && ($user_data['role'] == 'lehrer'))
   		{
   			$user_data = $this->session->userdata('logged_in');
   			$userid = $user_data['id'];
			$data['user'] = $this->user_model->get_user_id($userid);
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('users/changelehrer_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
	public function changeschueler_view()
	{
		$user_data = $this->session->userdata('logged_in');
		if($this->session->userdata('logged_in')  && ($user_data['role'] == 'schueler'))
   		{
   			$user_data = $this->session->userdata('logged_in');
   			$userid = $user_data['id'];
			$data['user'] = $this->user_model->get_user_id($userid);
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/schuelernavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('users/changeschueler_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}

	public function change()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Password', 'Password', 'trim|required|xss_clean');

		if ($this->form_validation->run() === FALSE)
		{	
			#redirect('user_controller','refresh');
			$this->change_view();
		}
		else
		{
			$userid = $this->input->post('userid');
			$this->user_model->change_user($userid);
			
			$user_data = $this->session->userdata('logged_in');
			
			if (($user_data['role'] == 'admin'))
				redirect('home/index', 'refresh');
			else if (($user_data['role'] == 'lehrer'))
				redirect('home/lehrerindex','refresh');
			else if (($user_data['role'] == 'schueler'))
				redirect('home/schuelerindex','refresh');
			//$this->userlistview();
		}
		 
	}
	
	public function validate()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Username', 'Username', 'trim|required|xss_clean');

		if ($this->form_validation->run() === FALSE)
		{	
			#redirect('user_controller','refresh');
			$this->validate_view();
		}
		else
		{
			$userid = $this->input->post('userid');
			$this->user_model->validate_user($userid);
			
			$this->index();
		}
		 
	}

	public function userlistview($page = 0)
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'admin'))
   		{	
			$limit = 5;
			$data['user'] = $this->user_model->get_all($limit, $page * $limit);
			$data['pages'] = ceil($this->user_model->get_count() / $limit);
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/adminnavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('users/userlist_view',$data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
}